//
//  main.m
//  作业7
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString *dataStr = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/作业7/作业7/area.txt" encoding:NSUTF8StringEncoding error:nil];
        NSArray *dataArray = [dataStr componentsSeparatedByString:@"\n"];
        NSMutableArray *country = [NSMutableArray array];
        for (NSString *line in dataArray) {
            if (![line hasPrefix:@" "]) {
                Province *pro = [[Province alloc] initWithName:line];
                [country addObject:pro];
            } else if (![line hasPrefix:@"   "]) {
                City *city = [[City alloc] initWithName:line];
                Province *pro = [country lastObject];
                NSMutableArray *arr = [pro arr];
                [arr addObject:city];
            } else {
                Province *pro = [country lastObject];
                NSMutableArray *cityArray = [pro arr];
                City *city = [cityArray lastObject];
                NSMutableArray *zoneArray = [city arr];
                Zone *zone = [[Zone alloc] initWithName:line];
                [zoneArray addObject:zone];
            }
        }
        NSLog(@"%@",country);
        
        
        NSString *dataStr1 = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/作业7/作业7/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
        NSArray *dataArray1 = [dataStr1 componentsSeparatedByString:@","];
        NSMutableArray *dicArray = [NSMutableArray array];
        for (NSString *line in dataArray1) {
            NSArray *arr = [line componentsSeparatedByString:@"|"];
            TempCity *tempCity = [[TempCity alloc] initWithName:[arr firstObject] number:[arr lastObject]];
            [dicArray addObject:tempCity];
        }
        
        
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
        [dicArray sortUsingDescriptors:@[sort]];
        NSLog(@"%@",dicArray);
        NSMutableArray *finallArray = [NSMutableArray array];
        for (TempCity *tempCity in dicArray) {
            if ([tempCity.number hasSuffix:@"0000"]) {
                Province *pro = [[Province alloc]initWitnTempCity:tempCity];
                [finallArray addObject:pro];
            } else if ([tempCity.number hasSuffix:@"00"]) {
                City *city = [[City alloc] initWitnTempCity:tempCity];
                [[[finallArray lastObject] arr] addObject:city];
            } else {
                Zone *zone = [[Zone alloc] initWitnTempCity:tempCity];
                [[[[[finallArray lastObject] arr] lastObject] arr] addObject:zone];
            }
        }
        NSLog(@"%@",finallArray);
    }
    return 0;
}
